﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.LapPhieu
{
    public partial class ThuTienGUI : UserControl, GUI.UctrlGUI
    {
        int _IDKhachHang;
        BLL.QuiDinhBLL _QuiDinhBLL;
        BLL.LapPhieuThuTienBLL _LapPhieuThuTienBLL;

        public ThuTienGUI()
        {
            InitializeComponent();
            _IDKhachHang = -1;
            _QuiDinhBLL = new BLL.QuiDinhBLL();
            _LapPhieuThuTienBLL = new BLL.LapPhieuThuTienBLL();
        }

        private void btn_TimKhachHang_Click(object sender, EventArgs e)
        {
            Form frm = GUI.qlnsGUI.isOpenForm(typeof(GUI.TraCuu_Update.KhachHangFrm));
            if (frm != null)
            {
                (frm as GUI.TraCuu_Update.KhachHangFrm).SetCallback(SetKhachHangFromTimKhachHang);
                frm.Focus();
            }
            else
            {
                frm = new GUI.TraCuu_Update.KhachHangFrm(SetKhachHangFromTimKhachHang);
                frm.Show(this);
            }
        }

        public String getName()
        {
            return "LẬP PHIẾU THU TIỀN";
        }

        private void SetKhachHangFromTimKhachHang(DTO.KhachHangDTO dto)
        {
            _IDKhachHang = dto.ID;
            txt_HoTen.Text = dto.HoTen;
            txt_DiaChi.Text = dto.DiaChi;
            txt_DienThoai.Text = dto.DienThoai;
            txt_Email.Text = dto.Email;
            txt_DuNo.Text = dto.DuNo.ToString();
        }

        private void ResetGui()
        {
            _IDKhachHang = -1;
            txt_HoTen.Text = String.Empty;
            txt_DiaChi.Text = String.Empty;
            txt_DienThoai.Text = String.Empty;
            txt_Email.Text = String.Empty;
            txt_DuNo.Text = String.Empty;
            txt_SoTien.Text = String.Empty;
        }

        private void btn_HoanTat_Click(object sender, EventArgs e)
        {
            if (_IDKhachHang < 0)
            {
                MessageBox.Show("Chọn Khách Hàng Trước");
                return;
            }

            if (txt_SoTien.Text==String.Empty)
            {
                MessageBox.Show("Số Tiền NULL");
                return;
            }

            int r = _LapPhieuThuTienBLL.LapPhieu(_IDKhachHang, dt_NgayThu.Value,
                float.Parse(txt_SoTien.Text));
            if (r != 0)
            {
                String msg = "Không Thể Lập Phiếu";
                if (_QuiDinhBLL.QD4)
                    msg += ", Số Tiền Thu Không Vượt Số Tiền Khách Hàng Nợ";
                MessageBox.Show(msg);
            }
            else
                ResetGui();
        }

        private void btn_LamLai_Click(object sender, EventArgs e)
        {
            ResetGui();
        }

        private void ThuTienGUI_Load(object sender, EventArgs e)
        {
            lb_QD4.Visible = _QuiDinhBLL.QD4;
            ResetGui();

            Form frm = GUI.qlnsGUI.isOpenForm(typeof(GUI.TraCuu_Update.KhachHangFrm));
            if (frm != null)
                (frm as GUI.TraCuu_Update.KhachHangFrm).SetCallback(SetKhachHangFromTimKhachHang);
        }

        private void txt_SoTien_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) &&
                !char.IsDigit(e.KeyChar) &&
                e.KeyChar != '.')
                e.Handled = true;
            if (e.KeyChar == '.' &&
                (sender as TextBox).Text.IndexOf('.') > -1)
                e.Handled = true;
        }
    }
}
